package cn.edu.web;

import cn.edu.pojo.Student;
import cn.edu.service.StudentService;
import cn.edu.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegistServlet extends BaseServlet {
    StudentService studentService = new StudentServiceImpl();

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("msgUsername","");
        req.setAttribute("msgPassword","");
         req.setAttribute("msgCode","");
        req.setAttribute("msgSex","");
         req.setAttribute("msgDate","");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean is = studentService.existSno(username);
        if (is){
            req.setAttribute("msgUsername","用户名已存在");
            req.setAttribute("username",username);
            req.getRequestDispatcher("/regist.jsp").forward(req,resp);
        }
        String regex="^[0-9]{6,18}$";
        System.out.println(password.matches(regex));
        if (!(password.matches(regex))){
            req.setAttribute("msgPassword","密码不符合规范");
            req.getRequestDispatcher("/regist.jsp").forward(req,resp);
        }
        String code = req.getParameter("code");
        if (!"abcd".equals(code)){
            req.setAttribute("msgCode","验证码错误");
            req.getRequestDispatcher("/regist.jsp").forward(req,resp);
        }
        String sex = req.getParameter("sex");
       if (sex==null){
           req.setAttribute("msgSex","请选择你的性别");
           req.getRequestDispatcher("/regist.jsp").forward(req,resp);
       }
       String date = req.getParameter("brith");
       if (date==null){
           req.setAttribute("msgDate","请选择你的出生日期");
           req.getRequestDispatcher("/regist.jsp").forward(req,resp);
       }

    }
}
